<template>
  <div class="eibs-tab">
    <c-col :span="12">
      <el-form-item label="渠道名称" prop="fromflg">
        <c-select v-model="sinConditon.fromflg" style="width: 100%" placeholder="请选择渠道名称" dbCode="FRMFLG"> </c-select>
      </el-form-item>
    </c-col>
    <c-col :span="12">
      <el-form-item label="签约状态" prop="sinsta">
        <c-select v-model="sinConditon.sinsta" style="width: 100%" placeholder="请选择签约状态" dbCode="SINSTA"> </c-select>
      </el-form-item>
    </c-col>
    <c-col :span="24">
      <c-col :span="12" style="text-align: left">
        <el-button type="primary" size="small" @click="signAdd" :disabled="isPageDisabled">新增</el-button>
      </c-col>
      <c-col :span="12" style="text-align: right">
        <el-button size="small" @click="handleReset">重置</el-button>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="small"
          @click="onSearch()"
          >查询
        </el-button>
      </c-col>
    </c-col>
    <c-col :span="24">
      <c-table :list="sinList" :columnsConfig="columns">
        <c-table-column fixed="right" prop="op" label="操作" width="200px">
          <template slot-scope="{ scope }">
            <c-button
              style="margin-left: 5px"
              size="small"
              type="primary"
              :disabled="isPageDisabled"
              @click="signEdit(scope.$index, scope.row)"
            >
              修改
            </c-button>
          </template>
        </c-table-column>
      </c-table>
    </c-col>
    <el-dialog
      :title="
        '签约信息:' +
        (operate === 'details' ? '详情' : operate === 'edit' ? '修改' : '新增')
      "
      :visible.sync="signDialog"
      top="10vh"
      width="80%"
      :destroy-on-close="true"
      :before-close="handleClose"
    >
      <m-sign-info ref="signInfo" :signInfo="signInfo" :operate="operate"></m-sign-info>
      <span slot="footer" class="dialog-footer">
        <button
          class="el-button el-button--default el-button--small"
          style="margin-left: 0"
          size="small"
          :disabled="false"
          @click.prevent="cancel"
        >
          <span>取 消</span>
        </button>
        <c-button type="primary" @click="editSignInf" v-if="operate === 'edit'"
          >保 存</c-button
        >
        <c-button type="primary" @click="saveSignInf" v-if="operate === 'add'"
          >保 存</c-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import codes from "~/config/CodeTable";
import { querySignList, addSign, editSign, querySignInfo } from "~/service/manage/sin.js";
import Sign from "./Signjs/Sign.js"
import SignInfo from "./SignInfo.vue";

export default {
  inject: ["root"],
  props: ['model','type'],
  data() {
    return {
      sign: null,
      signDialog: false,
      sinConditon: {
        fromflg: "",
        sinsta: "",
        ptyinr: "",
      },
      operate: "",
      sinList: [],
      signInfo: null,
      operateIdx: 0,
      columns: [
        { label: "渠道名称", prop: "fromflg", width: "auto" },
        { label: "客户编号", prop: "ptyextkey", width: "auto" },
        { label: "客户名称", prop: "ptynam", width: "auto" },
        { label: "统一社会信用代码", prop: "ptyidcode", width: "auto" },
        { label: "客户所属机构", prop: "branch", width: "auto" },
        { label: "签约状态", prop: "sinsta", width: "auto" },
        { label: "签约时间", prop: "sintime", width: "auto" },
        { label: "是否接受通知", prop: "ntfsta", width: "auto" },
        { label: "备注", prop: "remark", width: "auto" },
      ],
    };
  },
  components: {
    "m-sign-info": SignInfo
  },

  computed: {
    isPageDisabled() {
      if (this.type=="info") {
        return true
      }else if (this.type=="edit"){
        if  (this.$route.query.maaFlg != "Y" && this.$route.query.zFlg != "Y"){
          return true
        }
      }else if (this.type=="add"){
        return false
      }
      return false
    },
  },

  created() {
      
  },
  methods: {
    init() {
      const ptyinr = this.$route.query.inr;
      querySignList({"ptyinr": ptyinr}).then((res)=> {
        if (res.respCode == "AAAAAA") {
          this.$set(this, "sinList", res.data)
        } else {
          this.$notify.error("客户不存在")
        }
      });
    },
    getCodesByKey(key) {
      return codes[key] || [];
    },
    onSearch() {
      this.sinConditon.ptyinr = this.$route.query.inr
      querySignList(this.sinConditon).then((res) => {
        if (res.respCode == SUCCESS) {
          this.sinList = res.data
        }
      })
    },
    signAdd() {
      var signInfo = new Sign().data;
      signInfo.ptyextkey = this.model.extkey
      signInfo.nam = this.model.nam
      this.signInfo = signInfo
      this.operate = "add";
      this.signDialog = true;
    },
    signEdit(index, row) {
      querySignInfo(row).then((res) => {
        if (res.respCode == "AAAAAA") {
          this.signInfo = res.data
          this.signInfo.ptyextkey = this.model.extkey
          this.signInfo.nam = this.model.nam
        }
      })
      this.operate = "edit";
      this.operateIdx = index;
      this.signDialog = true;
    },
    cancel() {
      this.handleClose();
    },
    editSignInf() {
      this.signInfo.ptyextkey = this.model.extkey
      this.signInfo.ptynam = this.model.nam
      console.log(this.signInfo)
      editSign(this.signInfo).then((res) => {
        if(res.respCode == "AAAAAA") {
          this.$notify.success("修改成功!")
          this.handleClose();
          this.onSearch();
        }
      })
    },
    saveSignInf() {
      this.signInfo.ptyextkey = this.model.extkey
      this.signInfo.ptynam = this.model.nam
      addSign(this.signInfo).then((res) => {
        if (res.respCode == "AAAAAA") {
          this.$notify.success("新建成功!")
          this.handleClose();
          this.onSearch();
        }
      })
    },
    handleClose(done) {
      this.signDialog = false;
      if (done && typeof done === "function") {
        done();
      }
    },
    handleReset() {
      this.sinConditon = new Sign().data
    },
  }
};
</script>

<style>
</style>