<template>
  <template-page ref="pageRef" :getList="getList" :operation="operation()" :exportList="exportList"
    :columnParsing="columnParsing" :tableAttributes="tableAttributes" :tableEvents="tableEvents"
    :moreParameters="moreParameters">
    <div class="cartographer_big">
      <el-dialog title="详情" width="100%" :modal="false" :visible.sync="formBool" :before-close="handleClose">
        <zj-page-container v-if="formBool">
          <zj-page-fill class="neibuview">
            <zj-form-container ref="formRef" :form-data="formData" :formAttributes="{ 'label-position': 'top' }">
              <zj-form-module title="费用信息" :form-data="formData" :form-items="formItems">
              </zj-form-module>
              <zj-form-module v-if="formBool && formData.payType !== 'WECHAT'" title="审批信息" :form-data="formData"
                :form-items="spFormItems">
                <div v-if="openType === 1" style="text-align:right">
                  <el-button size="mini" type="danger" plain @click="WAITfun">确定</el-button>
                </div>
              </zj-form-module>
            </zj-form-container>
          </zj-page-fill>
        </zj-page-container>
      </el-dialog>
    </div>
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import operation_mixin from '@/components/template/operation_mixin.js'
import { enginMaterialList, enginMaterialListExport, enginMaterialDetail, enginMaterialExamine } from "@/api/applicationWithoutFee.js"
import { required } from '@/components/template/rules_verify.js'
import feel from "../mixins/feel.js"
export default {
  components: {
    TemplatePage,
  },
  mixins: [import_mixin, operation_mixin, feel],
  data() {
    return {
      formBool: false,
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: false,
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      formData: {
        examineStatus_cp: "",
        examineStatus: "",
        examineRemark: "",
      },
      openType: 0
    }
  },
  computed: {
    moreParameters() {
      return [
        {
          name: '状态',
          key: 'examineStatus',
          value: '',
          conditions: [
            { label: "全部", value: "" },
            { label: "待审核", value: "WAIT" },
            { label: "审核通过", value: "OK" },
            { label: "驳回", value: "FAIL" },
            { label: "取消", value: "NO" },
            { label: "已支付", value: "PAID" },
            { label: "未支付", value: "NO_PAID" },
          ]
        }
      ]
    },
    spFormItems() {
      return [{
        name: 'el-radio',
        md: 24,
        options: [{ value: "OK", label: "审批通过" }, { value: "FAIL", label: "审批驳回" }],
        attributes: { disabled: this.openType == 0 },
        formItemAttributes: { label: '审批状态', prop: 'examineStatus_cp', rules: [...required] },
      }, {
        name: 'el-input',
        md: 24,
        attributes: { disabled: this.openType == 0, type: "textarea", placeholder: '' },
        formItemAttributes: { label: '审批备注', prop: 'examineRemark' },
      }]
    }
  },
  created() {
    if (this.$route.query.id) {
      this.formType = 0
      this.getDetail(this.$route.query.id)
    }
  },
  methods: {
    // 列表请求函数
    getList(p, cb) {
      try {
        var pam = JSON.parse(JSON.stringify(p))
        pam.params.push({ "param": "is_all_fee", "compare": "=", "value": "NO" })
        if (pam.examineStatus) {
          pam.params.push({ "param": "examine_status", "compare": "=", "value": pam.examineStatus })
        }
        if (this.$route.query.rpProjectRepairId) {
          pam.params.push({ "param": "rp_project_repair_id", "compare": "=", "value": this.$route.query.rpProjectRepairId })
        }
        cb && cb(pam)
        return enginMaterialList(pam)
      } catch (err) {
        console.log(err)
      }
    },

    // 列表导出函数
    exportList: enginMaterialListExport,

    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },

    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },

    getDetail(id) {
      enginMaterialDetail({
        id
      }).then(res => {
        this.openType = 0
        this.formData = { ...res.data, examineStatus_cp: res.data.examineStatus }
        this.$nextTick(() => {
          this.formBool = true
        })
      })
    },

    operation() {
      return this.operationBtn({
        detail: {
          click: ({ row, index, column }) => {
            this.getDetail(row.orderId)
          }
        },
        examine: {
          conditions: ({ row, index, column }) => {
            return row.examineStatus == "WAIT"
          },
          click: ({ row, index, column }) => {
            enginMaterialDetail({
              id: row.orderId
            }).then(res => {
              this.openType = 1
              this.formData = { ...res.data, examineStatus_cp: res.data.examineStatus }
              this.$nextTick(() => {
                this.formBool = true
              })
            })
          }
        },
      })
    },

    handleClose() {
      this.$refs.formRef.$refs.inlineForm.clearValidate()
      this.formData = {}
      this.formBool = false
    },

    WAITfun() {
      this.$refs.formRef.validate((valid, invalidFields, errLabels) => {
        if (valid) {
          enginMaterialExamine({
            id: this.formData.orderId,
            examineStatus: this.formData.examineStatus_cp,
            examineRemark: this.formData.examineRemark
          }).then(res => {
            this.$refs.pageRef.refreshList()
            this.handleClose()
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.neibuview {
  box-sizing: border-box;
  padding-left: 16px;

  ::v-deep &>.zj-page-fill-scroll {
    box-sizing: border-box;
    padding-right: 16px;

    &>div:nth-child(1) {
      margin-top: 20px;
    }
  }
}
</style>